package com.htfyun.roobofactory;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;

/**
 * Created by sst on 2018/4/29.
 */

public class ViewDialogFragment extends DialogFragment {
    public interface Callback {
        void onClick();
    }

    private Callback cancelCb;
    private Callback okCb;

    private int titleResId = -1;
    private int msgResId = -1;

    private boolean isCancelable = true;

    public void show(FragmentManager fragmentManager, int titleResId, int msgResId, Callback okCb, Callback cancelCb) {
        show(fragmentManager, "ViewDialogFragment");

        this.titleResId = titleResId;
        this.msgResId = msgResId;
        this.cancelCb = cancelCb;
        this.okCb = okCb;
    }


    @Override
    public void onStart() {
        super.onStart();

        AlertDialog dialog = (AlertDialog) getDialog();
        dialog.setCancelable(isCancelable);

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(titleResId);
        builder.setMessage(msgResId);

        if (okCb != null) {
            builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (okCb != null) {
                        okCb.onClick();
                    }
                    dialog.dismiss();
                }
            });
        }

        if (cancelCb != null) {
            builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (cancelCb != null) {
                        cancelCb.onClick();
                    }
                    dialog.dismiss();
                }
            });

        }

        return builder.create();
    }

    @Override
    public void setCancelable(boolean cancelable) {
        isCancelable = cancelable;
    }
}
